package com.cy.sys.controller;

import com.cy.sys.common.pojo.JsonResult;
import com.cy.sys.common.until.PageUtil;
import com.cy.sys.pojo.Goods;
import com.cy.sys.service.GoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods/")
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @PostMapping
    @ApiOperation(value = "商品新增", notes = "接收新增商品信息")
    public JsonResult doSaveGoods(@RequestBody Goods entity){
        goodsService.saveGoods(entity);
        return new JsonResult("save ok");
    }
    @PutMapping
    @ApiOperation(value = "商品更新", notes = "接收更新后商品信息")
    public JsonResult doUpdateGoods(@RequestBody Goods entity){
        goodsService.updateGoods(entity);
        return new JsonResult("update ok");
    }
    @GetMapping("{id}")
    @ApiOperation(value = "通过id查询商品信息", notes = "接收id值")
    public JsonResult doFindById(@PathVariable Integer id){
        return new JsonResult(goodsService.findById(id));
    }
    @GetMapping
    @ApiOperation(value = "通过条件查询商品信息", notes = "接收商品信息")
    public JsonResult doFindGoods(Goods entity){
        return new JsonResult(PageUtil.startPage().doSelectPageInfo(()->{
                    goodsService.findGoods(entity);
        }));
    }
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除商品", notes = "接收id值")
    public JsonResult doDeleteGoodsById(@PathVariable Integer... id){
        goodsService.deleteGoodsById(id);
        return new JsonResult("delete ok");
    }
    @GetMapping ("/sale/{goodsId}/{saleQuantity}")
    @ApiOperation(value = "商品销售", notes = "接收商品id，销售数量")
    public JsonResult doSale(@PathVariable Integer goodsId,@PathVariable Integer saleQuantity){
        goodsService.doSale(goodsId,saleQuantity);
        return new JsonResult(1,"销售ok");
    }

}
